<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('Freeurl');
Globals::requireTable('Year');

class FreeController extends Controller
{
    protected $FreeurlTable;
    protected $YearTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->FreeurlTable = new FreeurlTable($config);
		$this->YearTable = new YearTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	//免费视频列表
	public function freevideoAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array(),'freevideotime'=>''));
	        exit();
	    }
	    
	    $pid = $this->getIntParam('pid');
	    
	    if($pid != 1374){
	        $date = date('Y-m-d H:i:s');
	        $year_data = $this->YearTable->getRow(" pid = '{$pid}' and uid = '{$uid}' and startdate < '{$date}' and enddate > '{$date}' ");
	        if((int)$year_data['id'] == 0){
	            echo json_encode(array('returncode'=>'-2','list'=>array(),'freevideotime'=>''));
	            exit();
	        }
	    }
	    
	    $freevideotime = $this->getParam('freevideotime');
	    
	    $list = array();
	    
	    $max_time = $this->FreeurlTable->getRowWithFields('createdate',array('status'=>1),'createdate desc');
	    if($max_time['createdate'] > $freevideotime){
	        $freevideotime = $max_time['createdate'];
	        
	        $data = $this->FreeurlTable->listAll(array('is_hidden'=>0),'rank ASC');
	        foreach ($data as $v){
	            $tmp = array();
	            $tmp['describe'] = $v['remark'];
	            $tmp['url'] = $v['activateurl'];
	            $tmp['title'] = $v['title'];
	            $tmp['icon'] = $v['icon'];
	            $tmp['createdate'] = $v['createdate'];
	            
	            $list[] = $tmp;
	        }
	    }
	    
	    
	    
	    echo json_encode(array('returncode'=>'0','list'=>$list,'freevideotime'=>$freevideotime));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('FreeController', 'Controller');
